package com.qf.mybatis01.utils;

import com.qf.mybatis01.dao.UserDao;
import com.qf.mybatis01.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {

    public static SqlSession getSqlSession() throws IOException {
        //如何创建SqlSession对象
        //把mybatis-config.xml配置文件转换成流对象
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //创建一个SqlSessionFactoryBuilder对象 创建一个SqlSessionFactory对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //利用SqlSessionFactory 对象可以创建SqlSession对象
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(resourceAsStream);
        // 核心对象 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
